using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using PHSRAG.Utility;
using SkeltaWorkFlow = Workflow.NET;
using System.Text;

namespace PHSRAG.Insight21.InsightWorkFlow
{
    /// <summary>
    /// this class will be used to prepare data for Skelta Wrokflow custom actioons and/or activities
    /// </summary>
    public class SWDataHelper
    {
        #region Constants
        private const string DefaultApplicationName = "EIRBWFApp";
        private const string WorkFlowName = "EIRBWFMain";
        internal const string anciBioBoardKey = "MGHAnimalsBioSafety";
        internal const string anciChemBoardKey = "MGHAnimalsSafety";
        internal const string anciRadBoardKey = "MGHAnimalsRadiationSafety";
        #endregion Constants

        #region Private variables
        private static string insightConnection = string.Empty;
        private static string insightApplicationName = string.Empty;        
        #endregion Private variables

        /// <summary>
        /// Insight application name retrieved from Skelta.Workflow.Config, if there is no entry,
        /// use the default value from this program
        /// </summary>
        public static string InsightApplicationName
        {
            get
            {
                if (string.Empty == insightApplicationName)
                {
                    insightApplicationName = Utility.Settings.GetKeyValue("InsightApplicationName", DefaultApplicationName);
                }
                return insightApplicationName;
            }
            set
            {
                insightApplicationName = value;
            }
        }

        /// <summary>
        /// Insight Connection string required by the application to run in skelta context 
        /// </summary>
        public static string InsightConnection
        {
            get
            {
                if (string.Empty == insightConnection)
                {
                    SkeltaWorkFlow.Config workflowConfig;
                    workflowConfig = new SkeltaWorkFlow.Config(InsightApplicationName);
                    insightConnection = workflowConfig.DataSourceConnectionString;
                }
                return insightConnection;
            }
        }

        /// <summary>
        /// This method Get Workflow Resources DataSet from Database, who will receive emails for Process request 	
        /// </summary>		
        /// <param name="executionID">Workflow Execution Id</param>
        /// <returns>Activity Id</returns> 
        internal static DataSet GetWorkflowResourcesData(int orgId, string workflowRole, string workflowType)
        {
            DataSet wfData = new DataSet();

            try {
                wfData = DataAccess.GetDataSet(InsightConnection,
					"GetWFResources",Utility.DataAccess.BuildSqlParameterArray(
                        "@OrganizationId", SqlDbType.Int, orgId
                        ,"@workflowRole", SqlDbType.VarChar, workflowRole
                        ,"@workflowType", SqlDbType.VarChar, workflowType
                     ));
            }
            catch (Exception ex)
            {
                throw new Exception("Error getting workflow resources data", ex);
            }

            return wfData;	
        }

        /// <summary>
        /// This method Get A list of Workflow Resources, who will receive emails for Process request 	
        /// </summary>		
        /// <param name="executionID">Workflow Execution Id</param>
        /// <returns>Activity Id</returns> 
        internal static ArrayList GetWorkflowResources(int orgId, string workflowRole)
        {
            ArrayList wfResources = new ArrayList();

            DataSet wfResourcesData = GetWorkflowResourcesData(orgId, workflowRole, String.Empty);

            if (wfResourcesData.Tables.Count > 0)
            {
                DataRowCollection wfDataRows = wfResourcesData.Tables[0].Rows;
                if (wfDataRows.Count > 0)
                {
                    foreach (DataRow wfDataRow in wfDataRows)
                    {
                        wfResources.Add(wfDataRow["ResourceID"]);
                    }
                }
            }

            return wfResources;
        }

        /// <summary>
        /// This method Get Workflow Resources DataSet from Database, who will receive emails for Process request 	
        /// </summary>		
        /// <param name="executionID">Workflow Execution Id</param>
        /// <returns>Activity Id</returns> 
        internal static void AppendArrayList(ArrayList a, ArrayList b)
        {
            foreach (Object item in b)
                a.Add(item);
        }

        ///<summary>
        /// this method will get the workflow user context variable after a workflow action, ie user context variable changed after certain action 
        /// retrieving workflow execution details data from Insight database
        /// </summary>
        /// <param name="executionId"></param>
        /// <param name="spName">stored procedure name</param>
        /// <param name="columnName"></param>
        /// <returns>void</returns>
        internal static string GetUserContextVariable(int executionDetailsId, string spName, string columnName)
        {
            SqlParameter[] args = DataAccess.BuildSqlParameterArray("@executionDetailsId", SqlDbType.Int, executionDetailsId);
            SqlDataReader reader = DataAccess.ExecuteReader(DataAccess.GetConnectionString(), spName, args);
            string contextVariable = ""; 

            try
            {                
                if (reader.Read())
                {
                    contextVariable = reader[columnName].ToString();
                }

            }
            catch (SqlException sqlException)
            {
                throw sqlException;
            }
            finally
            {
                reader.Close();
                reader.Dispose();
            }

            return contextVariable;
        }
    }
}
